﻿namespace Guru.Framework.Util
{
    using System;
    using System.Collections;
    using System.Xml;

    public class ConfUtil
    {
        private static readonly string XML_FILE_PATH = @"C:\CrmGuru\config\conf.xml";
        private static readonly string PROP_NODE = "appsettings/dblogin/db-properties/properties";
        private static Hashtable _properties;
        static ConfUtil()
        {
            _properties = GetProperties();
        }

        public static string Get(string key)
        {
            if (_properties.ContainsKey(key) == true)
            {
                object obj = _properties[key];
                return obj != null ? obj.ToString() : string.Empty;
            }
            else
                return string.Empty;
        }

        private static Hashtable GetProperties()
        {
            Hashtable ht = new Hashtable();
            return ht;
        }

        public static PropertyUtil BuildProperty(string propName)
        {
            try
            {
                // Buraya ekleme kısımları gelecek.
                PropertyUtil propUtil = new PropertyUtil();
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XML_FILE_PATH);
                XmlNodeList propNodeList = xmlDoc.SelectNodes(PROP_NODE);
                foreach (XmlNode node in propNodeList)
                {
                    if (node.Attributes.Count > 0)
                    {
                        if (node.Attributes["name"].InnerText.Trim().ToLower().Equals(propName) == true)
                        {
                            XmlNodeList xnlNdLst = node.SelectNodes("property");
                            foreach (XmlNode xml_node in xnlNdLst)
                            {
                                try
                                {
                                    propUtil.Add(
                                        SecurityUtil.DecodeString(xml_node.Attributes["key"].InnerText),
                                        SecurityUtil.DecodeString(xml_node.Attributes["value"].InnerText)
                                        );
                                }
                                catch
                                {
                                    continue;
                                }
                            }
                        }
                    }
                }

                return propUtil;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
